﻿using System;
using System.Collections.Generic;

using System.Text;
using EasyHook;
using System.Runtime.Remoting;
using System.Windows.Forms;
using Inject;
using System.Diagnostics;
using System.Drawing.Win32;

namespace Holdem.Inject
{
    public class StringEventArgs : EventArgs
    {
        public DateTime InTime;
        public string Message;

        public StringEventArgs(string msg, DateTime dt)
        {
            Message = msg;
            InTime = dt;
        }
    }

    public class FTChat
    {
        public WinWnd ChatWnd;
        public DateTime LastMessageTime;
        public string LastMessage;

        private EventHandler<StringEventArgs> _newMsg;

        public event EventHandler<StringEventArgs> NewMessage
        {
            add{ _newMsg+=value; }
            remove { _newMsg -= value; }
        }

        public FTChat(WinWnd wnd)
        {
            ChatWnd = wnd;
            InjectClient.InjectEvent += new EventHandler<CallEventArgs>(OnCall);
        }

        void OnCall(object sender, CallEventArgs e)
        {
            if (e.Function.StartsWith("ExtTextOut"))
            {
                if (e.InTime.CompareTo(LastMessageTime) > 0)
                {
                    string msg = e.Args[5] as string;
                    if(msg.StartsWith("Dealer:"))
                    {
                        LastMessageTime = e.InTime;
                        LastMessage = msg;
                        if(null!=_newMsg)
                            _newMsg(this, new StringEventArgs(LastMessage, LastMessageTime));
                    }
                }
            }
        }
    }

    public class FTMessageProxy
    {

        public FTMessageProxy()
        {
        }
    }

    public class TestInjectFT 
    {
        static string ChannelName = null;
        static string TargetName = "FullTiltPoker";
        static string AppName = "RunInject.exe";

        static WinWndMap WndMap = new WinWndMap();

        static FTChat FTChat;

        public static void All()
        {

            InjectClient.Inject(TargetName, AppName);

            InjectClient.InjectEvent += new EventHandler<CallEventArgs>(Instance_InjectEvent);
            InjectClient.WindowEvent += new EventHandler<WindowEventArgs>(Instance_WindowEvent);

            Console.ReadLine(); 
        }

        static void Instance_WindowEvent(object sender, WindowEventArgs e)
        {
            TestInjectFT.WndMap.Add(e.Wnd);
            WndMap.ResolveParents();
            Console.WriteLine(e.Wnd);

            if (e.Wnd.ClassName.Equals("FTCChat"))
            {
                Console.WriteLine("FTCChat Found {0}",e.Wnd);
                FTChat = new FTChat(e.Wnd);
                FTChat.NewMessage+=new EventHandler<StringEventArgs>(FTChat_NewMessage);
            }
        }


        static void FTChat_NewMessage(object sender, StringEventArgs e)
        {
            Console.WriteLine("FullTiltChat[{0}]: {1}",e.InTime, e.Message);
        } 
        
        static void Instance_InjectEvent(object sender, CallEventArgs e)
        {
            //Console.WriteLine(e);
        }

        public static void Main(string[] args)
        {
            All();
        }
    }
}
